var price = $('.total-price').text();

//weixin://wap/pay?
// appid=wx1bad553836a5f443&
// noncestr=zSoyvfO0CUhan8pv&
// package=WAP&
// prepayid=wx2016081511075994d57067ed0935023685&
// timestamp=1471230479&
// sign=2BFF2499365ACFF4250788552BCE2584

/**
 * 商品详情
 * @constructor
 */
function ProductDetail() {

    this.proId = pid;
    this.specId = 0;
    this.skuPrice = 0;

    this.ajaxUrl = {
        productDetail: "/product/{0}.do",//detail
        productSku: "/product/spec/{0}.do"
    }

    //变量
    this.variable = {
        $proImgSlides: $("#proImgSlides"),
        $proImgSlidesTemp: $("#proImgSlides-template"),
        $proDetailDesc: $("#proDetailDesc"), //desc
        $buyPrice: $("#buyPrice"), //price
        $totalPrice: $("#totalPrice"),
        $skuProperCheck: $("#skuProperCheck"),
        $skuProperPannel: $("#skuProperPannel"),
        $skuCheckBtn: $("#skuCheckBtn"),
        $dgscPc: $(".dgsc-pc"),
        $proImgColorSlidesTmp: $("#proImgColorSlides-template"),
        $proImgSizeSlidesTmp: $("#proImgSizeSlides-template"),
        $specColor: $("#specColor"),
        $specSize: $("#specSize")
    }

    this.common = new common()   //通用
}
ProductDetail.prototype.init = function () {
    var self = this;
    self.getData();
    self.getSkuOption();
}
ProductDetail.prototype.skuProper = function () {
    var self = this;
    self.variable.$skuProperCheck.click(function () {
        self.variable.$skuProperPannel.modal("open");
    });
    self.variable.$skuCheckBtn.click(function () {
        self.variable.$skuProperPannel.modal("close");
    });
    $(".dgsc-pc i").click(function () {
        var $skuTag = $(this);
        var tag = parseInt($skuTag.attr("data-tag"));
        if (tag === 0) {
            $skuTag.attr("style", "background-color: #ff4200;color:#fff");
            $skuTag.siblings().removeAttr("style");
            var skuPrice = $skuTag.attr("attr_price");
            self.specId = parseInt($skuTag.attr("attr_id"));
            $("#speId").attr("data", self.specId);
            var converPrice = parseInt(skuPrice.split(".")[0]);
            self.skuPrice = converPrice;

            var lfb = parseInt($("#lfb").text());
            var sjzf = converPrice - lfb;
            price = converPrice;
            self.variable.$totalPrice.text(sjzf);
            self.variable.$buyPrice.html("单价:" + skuPrice + "元");
        }
        else {
            $skuTag.removeAttr("style");
            $skuTag.attr("data-tag", 0);
        }
    });
}
/**
 * 加载sku选项
 */
ProductDetail.prototype.getSkuOption = function () {
    var self = this;
    $.ajax({
        type: "GET",
        url: self.ajaxUrl.productSku.format(self.proId),
        dataType: "json",
        success: function (data) {
            var $skuObj = self.common.evalJson(data);
            if (!!$skuObj) {
                var myTemplate = Handlebars.compile(self.variable.$proImgColorSlidesTmp.html());
                self.variable.$specColor.html(myTemplate($skuObj));
                self.skuProper();
            }
        }
    });
}

/**
 * 获取详情数据
 */
ProductDetail.prototype.getData = function () {
    var self = this;

    var user = JSON.parse(localStorage.getItem("user"));
    $.ajax({
        type: "GET",
        url: self.ajaxUrl.productDetail.format(self.proId),
        dataType: "json",
        data:{
            accountId:user.id
        },
        success: function (data) {

            for(var comment in data.pager.list){
                //alert(data.pager.list[comment].star);
                var starList="";
                for (var i=0;i<data.pager.list[comment].star;i++){
                    starList=starList+"⭐️";
                }
                data.pager.list[comment].starList=starList;
            }
            var myTemplate = Handlebars.compile($("#comment-template").html());
            $("#comment-content").html(myTemplate(data.pager));

            var $bannerObj = self.common.evalJson(data);
            if (!!$bannerObj) {
                var myTemplate = Handlebars.compile(self.variable.$proImgSlidesTemp.html());
                self.variable.$proImgSlides.html(myTemplate($bannerObj));


                var source = $('#shopname-template').html();
                var template = Handlebars.compile(source);
                var html = template($bannerObj);
                $("#shopName").append(html);
                $("#lfb").html($bannerObj.e.lfbdk);
                $("#totalLfb").html($bannerObj.e.lfb);


                $(".regular").slick({
                    arrows: false,
                    dots: true,
                    infinite: true,
                    slidesToShow: 1,
                    slidesToScroll: 1
                });

                $("#pay-btn").click(function () {

                    var pri = $(".total-price").html();
                    var num = parseInt($("#number").text());
                    // if (!!self.specId) {
                    //     pri = self.skuPrice;
                    // } else {
                    //     pri = $bannerObj.e.nowPrice;
                    // }

                    location.href = "/shop/app/ecshop/order.jsp?pid=" + pid + "&num=" + num + "&p=" + pri + "&spId=" + self.specId;
                });
                var totalPrice = parseInt($bannerObj.e.nowPrice.split(".")[0]);
                self.variable.$proDetailDesc.html($bannerObj.e.productHTML);

                self.variable.$buyPrice.html("单价:" + $bannerObj.e.nowPrice + "元");


                self.variable.$totalPrice.text(totalPrice-parseInt($bannerObj.e.lfbdk));

                price = totalPrice;
            }
        }
    });
}


$(function () {


    new ProductDetail().init();


    $('.sub').on('click', function () {
        if ($(this).hasClass('subtraction')) {
            return;
        }
        var num = parseInt($('.number').text());
        num--;
        if (num < 2) {
            $('.sub').addClass('subtraction').removeClass('subtractionabled');
        }
        if (num < 10) {
            $('.plus').addClass('plus-able').removeClass('plus-abled');
        }
        $('.number').text(num);
        var totalPrice = num * price;
        var splitPrice = totalPrice.toString().split('.');
        $('.sub-price').text(splitPrice[1] || '00');


        var lfb = $("#lfb").text() * num;
        var totalLfb = $("#totalLfb").text();
        if (totalLfb < lfb) {
            lfb = totalLfb;
        }

        var sjzf = totalPrice - lfb
        $('.total-price').text(sjzf);
        // $('#sjzf').text(sjzf);

    });


    $('.plus').on('click', function () {

        if ($(this).hasClass('plus-abled')) {
            return;
        }
        var num = parseInt($('.number').text());
        num++;
        if (num > 1) {
            $('.sub').addClass('subtractionabled').removeClass('subtraction');
        }
        if (num >= 10) {
            $('.plus').addClass('plus-abled').removeClass('plus-able');
        }
        $('.number').text(num);
        var totalPrice = num * price;
        var splitPrice = totalPrice.toString().split('.');
        $('.sub-price').text(splitPrice[1] || '00');


        var lfb = $("#lfb").text() * num;
        var totalLfb = $("#totalLfb").text();
        if (totalLfb < lfb) {
            lfb = totalLfb;
        }

        var sjzf = totalPrice - lfb
        $('.total-price').text(sjzf);

    });


    var $modal = $('#pay-modal');
    $('#pay-btn').on('click', function (e) {
        $modal.modal();
        $('body').on('click', '.am-close', function (e) {
            $modal.modal('close');
        });
    });


    $("#collect").click(function () {
        var url = "/h5/addToFavorite";
        var user = JSON.parse(localStorage.getItem("user"));
        $.post(url, {
            account: user.account,
            productID: pid
        }, function (result) {
            if (result.code == '000000') {
                alert("收藏成功")
            } else {
                alert(result.msg)
            }
        })

    })


    $("#shopping-cart").click(function () {
        var url = "/h5/addToBus";
        var user = JSON.parse(localStorage.getItem("user"));
        if (user == null || user == "null") {
            window.location.href = '../../app/login/login.jsp';
        }
        $.post(url, {
                account: user.account,
                productId: pid,
                spId: $("#speId").attr("data")
            },
            function (result) {
                if (result.code == '000000') {
                    alert("加入成功")
                } else {
                    alert(result.msg)
                }
            }
        )

    });

});